package com.heng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heng.dto.ProductDTO;
import com.heng.dto.res.R;
import com.heng.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 商品接口Controller（精选推荐、新品上架）
 */
@RestController
@RequestMapping("/api/products")
@RequiredArgsConstructor
@Validated  // 启用参数校验
@Tag(name = "商品接口", description = "精选推荐、新品上架相关接口")
@CrossOrigin("*")
public class ProductController {

    private final ProductService productService;

    /**
     * 精选推荐商品接口
     * @param pageNum 页码（可选，默认1）
     * @param pageSize 每页条数（可选，默认4）
     */
    @GetMapping("/featured")
    @Operation(summary = "精选推荐商品", description = "分页查询精选推荐商品，默认每页4条")
    public R<IPage<ProductDTO>> getFeaturedProducts(
            @Parameter(description = "页码") @RequestParam(required = false) Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(required = false) Integer pageSize
    ) {
        IPage<ProductDTO> result = productService.getFeaturedProducts(pageNum, pageSize);
        return R.success(result);
    }

    /**
     * 新品上架商品接口
     * @param pageNum 页码（可选，默认1）
     * @param pageSize 每页条数（可选，默认5）
     */
    @GetMapping("/new")
    @Operation(summary = "新品上架商品", description = "分页查询新品上架商品，默认每页5条")
    public R<IPage<ProductDTO>> getNewProducts(
            @Parameter(description = "页码") @RequestParam(required = false) Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(required = false) Integer pageSize
    ) {
        IPage<ProductDTO> result = productService.getNewProducts(pageNum, pageSize);
        return R.success(result);
    }
}